package com.zlm.IO.ByteStream.ZipOutputStream;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * @author 13026
 */
public class ZipOutputStream_demo {
    /*
     * ZipOutputStream
     * 压缩流
     * */
    public static void main(String[] args) throws IOException {
        // 1.创建一个File表示要解压的压缩包
        File src = new File("D:\\myProject\\javaTest\\oneModule\\src\\com\\zlm\\IO\\ByteStream\\ZipOutputStream\\FilesPackage\\aaa\\a.txt");
        // 2.创建一个File表示解压的目的地
        File dest = new File("D:\\myProject\\javaTest\\oneModule\\src\\com\\zlm\\IO\\ByteStream\\ZipOutputStream\\FilesPackage");
        toZip(src, dest);

    }

    public static void toZip(File src, File dest) throws IOException {
        // 1.创建一个压缩流来关联压缩包
        ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(new File(dest, "a.zip")));
        // 创建ZipEntry对象，代表文件/文件夹
        ZipEntry entry = new ZipEntry("a.txt");
        // 把ZipEntry放到压缩包中
        zos.putNextEntry(entry);

        // 将src中数据写入到压缩包中
        FileInputStream fis = new FileInputStream(src);
        int b;
        while ((b = fis.read()) != -1) {
            zos.write(b);
        }

        zos.closeEntry();
        zos.close();
    }
}
